/*
 * Copyright (c) 2010, Jim Hollinger
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Jim Hollinger nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <string>

#include "debughelper/debughelper.h"
#include "logger/logger.h"
#include "datachannel/socketchannel.h"


using namespace datachannel;


/**
 ****************************************************************************
 * @brief  Constructor
 ****************************************************************************
**/
SocketChannel::SocketChannel(logger::Logger *logger)
    : DataChannel(logger) {
    ip_port = 0;
    mode    = PASSIVE;
    type    = UDP;
}


/**
 ****************************************************************************
 * @brief  Destructor
 ****************************************************************************
**/
SocketChannel::~SocketChannel() {
}


/**
 ****************************************************************************
 * @brief  Process the descriptor and sets up internal object state.
 *
 * @param  descriptor  IP address to be opened
 *                      ip_address:port[:active|:passive][:tcp|:udp]
 *                      default = passive + udp
 ****************************************************************************
**/
void SocketChannel::processDescriptor(const std::string &descriptor) {
    size_t i = descriptor.find_first_of(':');
    if (i == std::string::npos) {
        ip_address = descriptor;
    } else {
        ip_address = descriptor.substr(0, i);
        if (isdigit(descriptor[i + 1])) {
            ip_port = strtol(descriptor.substr(i + 1).c_str(), NULL, 10);
        }
        if (descriptor.find(":active") != std::string::npos) {
            setSocketMode(ACTIVE);
        }
        if (descriptor.find(":passive") != std::string::npos) {
            setSocketMode(PASSIVE);
        }
        if (descriptor.find(":tcp") != std::string::npos) {
            setSocketType(TCP);
        }
        if (descriptor.find(":udp") != std::string::npos) {
            setSocketType(UDP);
        }
    }
    if (ip_address.compare("INADDR_ANY") == 0) {
        ip_address = "0.0.0.0";
    } else if (ip_address.compare("INADDR_LOOPBACK") == 0) {
        ip_address = "127.0.0.1";
    } else if (ip_address.compare("INADDR_BROADCAST") == 0) {
        ip_address = "255.255.255.255";
    } else if (ip_address.compare("INADDR_NONE") == 0) {
        ip_address = "255.255.255.255";
    }
}


/**
 ****************************************************************************
 * @brief  Sets the socket mode.
 *
 * @param  mode  Socket mode (active or passive)
 ****************************************************************************
**/
void SocketChannel::setSocketMode(SocketMode new_mode) {
    mode = new_mode;
}


/**
 ****************************************************************************
 * @brief  Sets the socket type.
 *
 * @param  type  Socket type (tcp or udp)
 ****************************************************************************
**/
void SocketChannel::setSocketType(SocketType new_type) {
    type = new_type;
}


/**
 ****************************************************************************
 * @brief  Returns the string representation of the socket type.
 *
 * @param  type  Socket type (tcp or udp)
 * @return char[] "TCP" or "UDP"
 ****************************************************************************
**/
const char *SocketChannel::typeToString(SocketType type) {
    const char *s = "UNKNOWN";

    switch (type) {

    case TCP:
        s = "tcp";
        break;

    case UDP:
        s = "udp";
        break;

    default:
        break;
    }

    return s;
}
